//
//  UIScrollView+DCAddStatusView.m
//  Demo
//
//  Created by zhaolin on 2023/2/3.
//

#import "UIScrollView+DCAddStatusView.h"

@implementation UIScrollView (DCAddStatusView)

/**
 TODO:添加状态view
 
 @param desString 状态描述文本
 @param statue 状态view的type
 */
- (DCStatusView *)addStatueViewWithDesString:(NSString *)desString statue:(DCStatusViewType)statue {
    [self removeStatuView];
    __block DCStatusView *statusView;
    didLayout(^{
        CGRect frame = self.bounds;
        statusView = [[DCStatusView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height) type:statue];
        statusView.backgroundColor = self.backgroundColor;
        [statusView setLabelDes:desString];
        [self addSubview:statusView];
    });
    return statusView;
}
/**
 TODO:移除状态view
 */
- (void)removeStatuView {
    for (UIView *subView in self.subviews) {
        if ([subView isKindOfClass:[DCStatusView class]]) {
            subView.hidden = YES;
            [subView removeFromSuperview];
        }
    }
}
/**
 解决刚加完约束的superview 直接添加statuView frame为0的问题
 */
void didLayout(void(^layout)(void)) {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (layout) layout();
    });
}
@end
